package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DeptFile;
import com.ruoyi.system.mapper.DeptFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DeptFileUserStoreMapper;
import com.ruoyi.system.domain.DeptFileUserStore;
import com.ruoyi.system.service.IDeptFileUserStoreService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 文件收藏Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-17
 */
@Service
public class DeptFileUserStoreServiceImpl implements IDeptFileUserStoreService
{
    @Autowired
    private DeptFileUserStoreMapper deptFileUserStoreMapper;
    @Autowired
    private DeptFileMapper deptFileMapper;


    /**
     * 查询文件收藏列表
     *
     * @param deptFile 文件收藏
     * @return 文件收藏
     */
    @Override
    public List<DeptFile> selectDeptFileUserStoreList(DeptFile deptFile)
    {
//        ArrayList<DeptFile> resultList = new ArrayList<>();
//        List<DeptFileUserStore> deptFileUserStores = deptFileUserStoreMapper.selectDeptFileUserStoreList(deptFileUserStore);
//        for(DeptFileUserStore file : deptFileUserStores){
//            DeptFile deptFile = deptFileMapper.selectDeptFileByFileId(file.getFileId());
//            resultList.add(deptFile);
//        }
//        return resultList;
        List<DeptFile> deptFiles = deptFileUserStoreMapper.selectDeptFileUserStoreList(deptFile);
        return deptFiles;
    }

    /**
     * 取消收藏
     *
     * @param fileId 部门文件主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int cancelStoreFile(Long fileId){
        List<DeptFile> deptFiles = deptFileMapper.selectChildrenDeptFileByFileId(fileId);
        for(DeptFile file :deptFiles ){
            deptFileUserStoreMapper.deleteDeptFileUserStoreById(file.getFileId());
        }
        return deptFileUserStoreMapper.deleteDeptFileUserStoreById(fileId);
    }
}
